<?php
namespace Home\Controller;
use Think\Controller;
class OrderController extends Controller
{
    /**********书籍订单列表*********/
    public function BookOrderList(){
        $userID=isset($_POST['userId'])?trim($_POST['userId']):null;
        $page=isset($_POST['page'])?trim($_POST['page']):1;
        $model=M('book_order');
        $map['user_id']=$userID;
        $orderlist=$model->where($map)->field('order_id,goods_name,single_price,count,date,status,total_price')->page($page,5)->select();
        if(empty($orderlist)){
           $requset=array('returnCode'=>999,'returnMsg'=>'空数据','returnData'=>null);
        }else{
           $requset=array('returnCode'=>0,'returnMsg'=>'返回成功','returnData'=>$orderlist);
        }
        echo json_encode($requset); exit;                            //$this->ajaxReturn($requset);
    }

    /**********生成书籍订单*********/
    public function addBookOrder(){
        $user_id=isset($_POST['userId'])?trim($_POST['userId']):null;
        $goods_id=isset($_POST['goods_id'])?trim($_POST['goods_id']):null;
        $IsShare=isset($_POST['IsShare'])?trim($_POST['IsShare']):null;
        $count=isset($_POST['count'])?trim($_POST['count']):null;
        $consignee=isset($_POST['consignee'])?trim($_POST['consignee']):null;
        $tel=isset($_POST['tel'])?trim($_POST['tel']):null;
        $address=isset($_POST['address'])?trim($_POST['address']):null;
        $date=time();
        $requset=array();
        if(empty($user_id)||empty($goods_id)||empty($count)||empty($consignee)||empty($tel)||empty($address)||empty($IsShare)){
            $requset=array('returnCode'=>997,'returnMsg'=>'所请求数据信息不完整','returnData'=>null);
            echo json_encode($requset); exit;
        }else{
             $re=M('User')->where('id='.$user_id)->field('nickname')->find();
             $nickname=$re['nickname'];
             $re=M('Book')->where('id='.$goods_id)->field('name,price')->find();
             $goods_name=$re['name'];
             $single_price=($IsShare==1?$re['price']:$re['price']-$re['minus']);                   //如果分享 就按分享价格购买
             if(empty($nickname)||empty($single_price)||empty($goods_name)){
              $requset=array('returnCode'=>998,'returnMsg'=>'所请求数据信息有错','returnData'=>null);
              echo json_encode($requset); exit;
             }else{
                 $total_price=$count*$single_price;
                 $order_id=date('Ymdhis',time()).rand(1000,9999);
                 $data['nick_name']=$nickname;
                 $data['order_id']=$order_id;
                 $data['user_id']=$user_id;
                 $data['single_price']=$single_price;
                 $data['count']=$count;
                 $data['total_price']=$total_price;
                 $data['date']=$date;
                 $data['consignee']=$consignee;
                 $data['tel']=$tel;
                 $data['address']=$address;
                 $data['status']=4;
                 $data['goods_name']=$goods_name;
                 if(M('Book_order')->add($data)){
                   $requset=array('returnCode'=>0,'returnMsg'=>'成功','returnData'=>$data);
                 }else{
                   $requset=array('returnCode'=>999,'returnMsg'=>'失败');
                 }
                 echo json_encode($requset); exit;
             }
        }
    }
    
    /**********生成其他订单(题库，资料)*********/
    
    public function addOtherOrder(){
        $user_id=isset($_POST['userId'])?trim($_POST['userId']):null;
        $type=isset($_POST['type'])?trim($_POST['type']):null;    //2.购买资料  4.购买题库
        $consignee=isset($_POST['consignee'])?trim($_POST['consignee']):null;
        $tel=isset($_POST['tel'])?trim($_POST['tel']):null;
        $address=isset($_POST['address'])?trim($_POST['address']):null;
        $date=time();
        $requset=array();
        if(empty($user_id)||empty($goods_id)||empty($count)||empty($consignee)||empty($tel)||empty($address)){
            $requset=array('returnCode'=>997,'returnMsg'=>'所请求数据信息不完整','returnData'=>null);
            echo json_encode($requset); exit;
        }else{
            $re=M('User')->where(array('user_id'=>$user_id))->field('nickname,')->find();
            $nickname=$re['nickname'];
            $re=M('Price')->where(array('type'=>$type))->field('price,name')->find();
            $goods_name=$re['name'];
            $single_price=$re['price'];
            if(empty($nickname)||empty($single_price)||empty($goods_name)){
                $requset=array('returnCode'=>998,'returnMsg'=>'所请求数据信息有错','returnData'=>null);
                echo json_encode($requset); exit;
            }else{
                $total_price=$single_price;
                $order_id=date('Ymdhis',time()).rand(1000,9999);
                $data['nickname']=$nickname;
                $data['order_id']=$order_id;
                $data['user_id']=$user_id;
                $data['single_price']=$single_price;
                $data['count']=$count;
                $data['total_price']=$total_price;
                $data['date']=$date;
                $data['consignee']=$consignee;
                $data['tel']=$tel;
                $data['address']=$address;
                $data['status']=4;
                $data['order_type']=$type;
                $data['goods_name']=$goods_name;
                if(M('Order')->add($data)){
                    $requset=array('returnCode'=>0,'returnMsg'=>'成功','returnData'=>$data);
                }else{
                    $requset=array('returnCode'=>999,'returnMsg'=>'失败');
                }
                echo json_encode($requset); exit;
            }
        }
    }
    /**************购买押秘**************/
    public function testKeyOrder(){
        $keyId = !empty($_POST['keyId'])?($_POST['keyId']):null;       //分类id
        $userId = isset($_POST['userId'])?trim($_POST['userId']):null;       //用户id
        $name = isset($_POST['name'])?trim($_POST['name']):null;       //用户id
        $phone = isset($_POST['phone'])?trim($_POST['phone']):null;       //用户id
        $cardNo = isset($_POST['cardNo'])?trim($_POST['cardNo']):null;       //用户id
        $catId = isset($_POST['catId'])?trim($_POST['catId']):null;       //用户id
        //var_dump($keyId);exit;
        if (empty($keyId) || !is_array($keyId) || count($keyId) < 1 ){
            $requset=array(
                'returnCode' => 998,
                'returnMsg' => '参数错误'
            );
            echo json_encode($requset); exit;
        }
        $order_id=time().rand(100000,999999);
        $model=M('Testkey_order');$testkey=M('Testkey');
        $testkeyli=$testkey->field('cata_id')->where('id='.$keyId['0'])->find();
        $testli=$testkey->field('id')->where('cata_id='.$testkeyli['cata_id'])->select();
        $testli=array_two_one($testli,'id');
        $keyidCount=count($keyId);$keylistCount=count($testli);
        if($keyidCount == $keylistCount){
            $keyIdStr=implode(',',$keyId );
            $data=array(
                'user_id'=>$userId,
                'name'=>$name,
                'phone'=>$phone,
                'cardNo'=>$cardNo,
                'key_id'=>$keyIdStr,
                'price'=>$testkeyli['price'],
                'cat_id'=>$catId,
				'status'=>0,
            );
	$orderlist=$model->where($data)->find();
            if(empty($orderlist)){
                $data['order_id']=$order_id;
                $data['addtime']=time();
                $model->add($data);
            }else{
                $order_id=$orderlist['order_id'];
            }

        }else{
            $keyIdStr=implode(',',$keyId );
            $totalPrice=0;
            foreach ($keyId as $val){
                $testpli=$testkey->field('price')->where('id='.$val)->find();
                $totalPrice+=$testpli['price'];
            }
            $data=array(
                'user_id'=>$userId,
                'name'=>$name,
                'phone'=>$phone,
                'cardNo'=>$cardNo,
                'key_id'=>$keyIdStr,
                'price'=>$totalPrice,
				'status'=>0,
            );
            $orderlist=$model->where($data)->find();
            if(empty($orderlist)){
                $data['order_id']=$order_id;
                $data['addtime']=time();
                $model->add($data);
            }else{
                $order_id=$orderlist['order_id'];
            }
        }

        echo json_encode(array('returnCode'=>0,'returnMsg'=>'成功','returnData'=>array('orderId'=>$order_id))); exit;
    }
    /*************购买资料**************/
    public function datumOrder(){
        $catId = isset($_POST['catId'])?trim($_POST['catId']):null;       //分类id
        $userId = isset($_POST['userId'])?trim($_POST['userId']):null;       //用户id
        $model=M('Datum_order');$price=M('Price');
        $priceli=$price->field('reference_price')->where('cate_id='.$catId)->find();
        $order_id=time().rand(100000,999999);
        $data=array(
            'user_id'=>$userId,
            'cat_id'=>$catId,
            'price'=>$priceli['reference_price'],
            'status'=>0,
        );
        $orderli=$model->where($data)->find();
        if(!empty($orderli)){
            echo json_encode(array('returnCode'=>0,'returnMsg'=>'成功','returnData'=>array('orderId'=>$orderli['order_id']))); exit;
        }else{
            $data['order_id']=$order_id;
            $data['addtime']=time();
            if(!$model->add($data)){
                echo json_encode(array('returnCode'=>999,'returnMsg'=>'失败')); exit;
            }else{
                echo json_encode(array('returnCode'=>0,'returnMsg'=>'成功','returnData'=>array('orderId'=>$order_id))); exit;
            }
        }
    }
    public function questionOrder(){
        $catId = isset($_POST['catId'])?trim($_POST['catId']):null;       //分类id
        $userId = isset($_POST['userId'])?trim($_POST['userId']):null;       //用户id
        if (empty($catId)){
            $requset=array(
                'returnCode' => 998,
                'returnMsg' => '参数错误'
            );
            echo json_encode($requset); exit;
        }
        $model=M('Questions_order');$price=M('Price');
        $priceli=$price->field('exam_price')->where('cate_id')->find();
        $order_id=time().rand(100000,999999);
        $data=array(
            'user_id'=>$userId,
            'cat_id'=>$catId,
            'price'=>$priceli['exam_price'],
			'status'=>0,
        );
        $orderli=$model->where($data)->find();
        if(!empty($orderli)){
            echo json_encode(array('returnCode'=>0,'returnMsg'=>'成功','returnData'=>array('orderId'=>$orderli['order_id']))); exit;
        }else{
            $data['order_id']=$order_id;
            $data['addtime']=time();
            if(!$model->add($data)){
                echo json_encode(array('returnCode'=>999,'returnMsg'=>'失败')); exit;
            }else{
                echo json_encode(array('returnCode'=>0,'returnMsg'=>'成功','returnData'=>array('orderId'=>$order_id))); exit;
            }
        }
    }
}